/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Manpreet
 */
public class Traffic {

    private int x1 = 1;
    private int x2 = 1;
    private int x3 = 1;
    private int y1 = 1;
    private int y2 = 1;
    private int y3 = 1;
    private int y4 = 1;
    private Color color;

    /**
     * this class is used to create the cars and assign them values to where they will be printed and which ArrayList they will be put in
     * @param carX
     * @param carY
     * @param X
     * @param Y
     * @param Xcars
     * @param Ycars
     * @param getHeight
     * @param getWidth
     */
    public void run(Car carX, Car carY, Street X, Street Y, ArrayList<Car> Xcars, ArrayList<Car> Ycars, int getHeight, int getWidth) {
        Random RanCol = new Random();
        int randomIndex = RanCol.nextInt( 3 );
        if (randomIndex == 0){
            color = Color.BLUE;
        }

        if (randomIndex == 1){
            color = Color.YELLOW;
        }

        if (randomIndex == 2){
            color = Color.cyan;
        }
        

        if (X.getLaneNum() == 1) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 - 10);
                c.setLane(1);
                c.setNum(x1);
                c.setColor(color);
                x1++;
                Xcars.add(c);

            }
        }
        if (X.getLaneNum() == 2) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 + 5);
                c.setLane(2);
                c.setNum(x2);
                c.setColor(color);
                x2++;
                Xcars.add(c);
            }

            double b = generator.nextDouble();
            if (b < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 - 25);
                c.setLane(1);
                c.setNum(x1);
                c.setColor(color);
                x1++;
                Xcars.add(c);

            }
        }
        if (X.getLaneNum() == 3) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 + 20);
                c.setLane(3);
                c.setNum(x3);
                c.setColor(color);
                x3++;
                Xcars.add(c);

            }
            double b = generator.nextDouble();
            if (b < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 - 10);
                c.setLane(2);
                c.setNum(x2);
                c.setColor(color);
                x2++;
                Xcars.add(c);

            }
            double d = generator.nextDouble();
            if (d < X.getProb()) {
                Car c = new Car();
                c.setCarY(getHeight / 2 - 40);
                c.setLane(1);
                c.setNum(x1);
                c.setColor(color);
                x1++;
                Xcars.add(c);

            }
        }

        if (Y.getLaneNum() == 1) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 10);
                c.setLane(1);
                c.setNum(y1);
                c.setColor(color);
                y1++;
                Ycars.add(c);

            }
        }
        if (Y.getLaneNum() == 2) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 + 5);
                c.setLane(2);
                c.setNum(y2);
                c.setColor(color);
                y2++;
                Ycars.add(c);
            }

            double b = generator.nextDouble();
            if (b < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 25);
                c.setLane(1);
                c.setNum(y1);
                c.setColor(color);
                y1++;
                Ycars.add(c);

            }
        }
        if (Y.getLaneNum() == 3) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 + 20);
                c.setLane(3);
                c.setNum(y3);
                c.setColor(color);
                y3++;
                Ycars.add(c);

            }
            double b = generator.nextDouble();
            if (b < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 10);
                c.setLane(2);
                c.setNum(y2);
                c.setColor(color);
                y2++;
                Ycars.add(c);

            }
            double d = generator.nextDouble();
            if (d < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 40);
                c.setLane(1);
                c.setNum(y1);
                c.setColor(color);
                y1++;
                Ycars.add(c);

            }
        }

        if (Y.getLaneNum() == 4) {
            Random generator = new Random();
            double r = generator.nextDouble();
            if (r < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 + 30);
                c.setLane(4);
                c.setNum(y4);
                c.setColor(color);
                y4++;
                Ycars.add(c);

            }
            double b = generator.nextDouble();
            if (b < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 + 5);
                c.setLane(3);
                c.setNum(y3);
                c.setColor(color);
                y3++;
                Ycars.add(c);

            }
            double d = generator.nextDouble();
            if (d < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 25);
                c.setLane(2);
                c.setNum(y2);
                c.setColor(color);
                y2++;
                Ycars.add(c);

            }
            double j = generator.nextDouble();
            if (j < Y.getProb()) {
                Car c = new Car();
                c.setCarX(getWidth / 2 - 50);
                c.setLane(1);
                c.setNum(y1);
                c.setColor(color);
                y1++;
                Ycars.add(c);

            }
            
        }
        for (int i = 0; i < Xcars.size(); i++) {
            if (Xcars.get(i).getColor() == Color.BLUE){
                Xcars.get(i).setHeight(30);
            }
        }
        for (int i = 0; i < Xcars.size(); i++) {
            if (Xcars.get(i).getColor() == Color.cyan){
                Xcars.get(i).setHeight(50);
            }
        }
                for (int i = 0; i < Ycars.size(); i++) {
            if (Ycars.get(i).getColor() == Color.BLUE){
                Ycars.get(i).setLength(30);
            }
        }
        for (int i = 0; i < Ycars.size(); i++) {
            if (Ycars.get(i).getColor() == Color.cyan){
                Ycars.get(i).setLength(50);
    }
        }
    }
}
